package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author fanYuLe
 * @Date 2023/3/1 11:35
 * @Blog blog.fanYuLe.net/996.mba
 * @Description:
 */
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private HttpSession session;
    //1.登录
    @Override
    public ResultInfo login(String username, String password) {
        //1.现根据用户名查询数据库
       Employee employee = employeeMapper.findByUsername(username);
        //2.判断是否查到
        if(employee == null){
            return ResultInfo.error("此用户不存在");
        }
        //3.比较密码
        //  3-1将密码转为密文
        String md5 = SecureUtil.md5(password);
        //  3-2比较
        if(!StrUtil.equals(md5,employee.getPassword())){
            return ResultInfo.error("密码错误");
        }
        //4.判断员工状态
        if(employee.getStatus()!=1){
            return ResultInfo.error("此账号被禁用，请联系管理员");
        }
        //5.登录成功
        session.setAttribute(Constant.SESSION_EMPLOYEE,employee);
        //6.返回成功结果
        return ResultInfo.success(employee);
    }

    //2.根据姓名查询
    @Override
    public List<Employee> find(String name) {
        List<Employee> list=employeeMapper.find(name);
        return list;
    }

    //3.新增员工
    @Override
    public void save(Employee employee) {
        //1.补全信息
            //1-1雪花ID
        long employeeId = IdUtil.createSnowflake(1, 1).nextId();
        employee.setId(employeeId);
            //1-2初始密码
        String md5 = SecureUtil.md5(Constant.INIT_PASSWORD);
        employee.setPassword(md5);
            //1-3员工状态
        employee.setStatus(Employee.STATUS_ENABLE);
         /*   //1-4创建、修改时间
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
            //1-5创建修改人
        Employee currentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
        employee.setCreateUser(currentEmp.getId());
        employee.setUpdateUser(currentEmp.getId());*/
        //2.mapper保存
        employeeMapper.save(employee);
    }

    //4.回显员工
    @Override
    public Employee findById(Long id) {
        Employee employee=employeeMapper.findById(id);
        return employee;
    }
    //5.修改员工
    @Override
    public void update(Employee employee) {
        //1.补充信息
            //1-1修改时间
        /*employee.setUpdateTime(new Date());
            //1-2修改人
        Employee currentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
        employee.setUpdateUser(currentEmp.getId());*/
        //2.调用mapper
        //employeeMapper.update(employee);
        //将更新方法改成mybatisPlus提供的
        employeeMapper.updateById(employee);
    }
}

